package com.mass.core.sys.menu.services.impl;

import com.mass.core.sys.authority.model.SysAuthority;
import com.mass.core.sys.authority.services.SysAuthorityServices;
import com.mass.core.sys.menu.dao.SysMenuMapper;
import com.mass.core.sys.menu.model.SysMenu;
import com.mass.core.sys.menu.model.SysMenuExample;
import com.mass.core.sys.menu.services.SysMenuServices;
import com.mass.core.sys.role.model.SysRole;
import com.mass.core.sys.user.model.SysUser;
import com.mass.core.utils.AjaxResponse;
import com.mass.core.utils.DateUtils;
import com.mass.core.utils.WebHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class SysMenuServicesImpl implements SysMenuServices {

    @Autowired
    private SysMenuMapper sysMenuMapper;    //菜单dao层接口


    @Autowired
    private SysAuthorityServices sysAuthorityServices; //权限server接口

    /**
     * @return List<SysMenu> 返回类型
     * @throws
     * @Title: findAll
     * @Description: 查询所有有效菜单
     */

    public List<SysMenu> findAll() {
        SysMenuExample smx = new SysMenuExample();
        SysMenuExample.Criteria criteria = smx.createCriteria();
        //正常数据
        criteria.andDelFlagEqualTo(0);
        //排序
        smx.setOrderByClause("PARENT_ID,SORT_INDEX,MENU_LEVEL");
        List<SysMenu> sysMenuList = sysMenuMapper.selectByExample(smx);
        return sysMenuList;
    }

    /**
     * @param userId 用户id
     * @return int 返回类型
     * @throws
     * @Title: deleteSysMenuTempByUserId
     * @Description: 根据用户id 删除临时表中的数据
     */

    public AjaxResponse selectSysMenuPage(SysMenu sysMenu, Integer pageIndex,
                                          Integer pageSize) {
        SysMenuExample smx = new SysMenuExample();
        SysMenuExample.Criteria criteria = smx.createCriteria();
        if (!ObjectUtils.isEmpty(sysMenu)) {
            if (!ObjectUtils.isEmpty(sysMenu.getId())) {
                criteria.andParentIdEqualTo(sysMenu.getId());
                SysMenuExample.Criteria criteria1 = smx.createCriteria();
                criteria1.andIdEqualTo(sysMenu.getId());
                smx.or(criteria1);
            }
            if (!ObjectUtils.isEmpty(sysMenu.getMenuName())) {
                criteria.andMenuNameLike("%" + sysMenu.getMenuName() + "%");
            }
            if (!ObjectUtils.isEmpty(sysMenu.getMenuCode())) {
                criteria.andMenuCodeLike("%" + sysMenu.getMenuCode() + "%");
            }
        }
        //正常数据
        criteria.andDelFlagEqualTo(0);
        //排序
        smx.setOrderByClause("PARENT_ID,SORT_INDEX,MENU_LEVEL");
        smx.setPageIndex((pageIndex - 1) * pageSize);
        smx.setPageSize(pageSize);
        List<SysMenu> sysMenuList = sysMenuMapper.selectByExample(smx);
        Long count = sysMenuMapper.countByExample(smx);
        return AjaxResponse.success("ok", sysMenuList, count, pageIndex);
    }


    @Transactional
    public AjaxResponse insertSysMenu(SysMenu sysMenu, HttpSession session) {
        sysMenu.setEnabled(1);     //设置是否启用
        sysMenu.setDelFlag(0);
        sysMenu.setCreateTime(DateUtils.getDate());
        //添加菜单
        int count = sysMenuMapper.insertSelective(sysMenu);
        if (count > 0) {
            SysUser sysUser = WebHelper.getSessionAttribute(session, "sysUser", SysUser.class);
            if (!ObjectUtils.isEmpty(sysMenu.getId()) && !ObjectUtils.isEmpty(sysUser)) {
                SysAuthority sysAuthority = new SysAuthority();
                sysAuthority.setMenuId(sysMenu.getId());
                if (!ObjectUtils.isEmpty(sysUser.getRoleId()) && sysUser.getRoleId() > 0) {
                    sysAuthority.setTypes(1);  //角色
                    sysAuthority.setRoleOrUserId(sysUser.getRoleId());
                } else {
                    List<SysRole> sysRoleList = sysUser.getSysRoleList();
                    if (!ObjectUtils.isEmpty(sysRoleList) && sysRoleList.size() > 0) {
                        sysAuthority.setTypes(1);  //角色
                        sysAuthority.setRoleOrUserId(sysRoleList.get(0).getId());
                    } else {
                        sysAuthority.setTypes(2);  //用户
                        sysAuthority.setRoleOrUserId(sysUser.getId());
                    }
                }
                //添加权限
                sysAuthorityServices.insertSysAuthority(sysAuthority);

                //管理员角色权限
                if (sysAuthority.getRoleOrUserId() != 1) {
                    sysAuthority.setRoleOrUserId((long) 1);
                    //添加权限
                    sysAuthorityServices.insertSysAuthority(sysAuthority);
                }
                return AjaxResponse.success("ok");
            } else {
                return AjaxResponse.error("no", "添加权限失败");
            }
        } else {
            return AjaxResponse.error("no");
        }
    }

    /**
     * 修改菜单
     *
     * @param sysMenu
     * @return AjaxResponse
     * @Param moveType 移动位置
     */

    @Transactional
    public AjaxResponse updateSysMenu(SysMenu sysMenu, String moveType) {
        sysMenu.setLastUpdateTime(DateUtils.getDate());
        int count = sysMenuMapper.updateByPrimaryKeySelective(sysMenu);
        if (count > 0 && !ObjectUtils.isEmpty(moveType)) {//排序
            sysMenuMapper.updateSortIndexAdd(sysMenu);
        }
        return AjaxResponse.success(count);
    }

    @Override
    public AjaxResponse findSysMenu(Long id) {
        return AjaxResponse.success(sysMenuMapper.selectByPrimaryKey(id));
    }

}
